package Sever端;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class SQLDeal {
    MyConSQL conn = new MyConSQL();
    Map<String, String> userMap = new HashMap<>();

    public SQLDeal() {
        ResultSet rs = conn.getResult("select uid , name from userlist");
        try {
            while (rs.next()) userMap.put(rs.getString("uid"), rs.getString("name"));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public String check_account(String cmd) {//检查正确与否
        String[] str = cmd.split("&");
        String id = str[0];
        String password = str[1];
        String sql = "select password from userlist where uid = " + id;//获得密码进行对照
        ResultSet rs = conn.getResult(sql);
        String ps = null;
        try {
            if (rs.next()) ps = rs.getString(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (ps == null || !ps.equals(password)) return "" + false;
        else return "" + true;
    }

    public String get_info(String cmd) {
        //获取id1与id2在preTime之后的的消息记录
        String[] str = cmd.split("&");
        final StringBuilder sql = new StringBuilder("select senderId,nowtime,message from information where ");
        if (str.length == 4) {
            String id1 = str[0];
            String id2 = str[1];
            String preTime = str[2];
            String time2 = str[3];
            sql.append("nowtime >= '").append(preTime).append("' and ")
                    .append("nowtime < '").append(time2).append("' and ((")
                    .append("senderId= '").append(id1).append("' and receiverId='").append(id2).append("')or(")
                    .append("senderId= '").append(id2).append("' and receiverId='").append(id1).append("'))");
        } else {
            String gid = str[0];
            String preTime = str[1];
            String time2 = str[2];
            sql.append("nowtime >= '").append(preTime).append("' and nowtime <'").append(time2)
                    .append("' and ").append("receiverId='").append(gid).append("'");
        }
        ResultSet rs = conn.getResult(sql.toString());
        String name, time, text;
        StringBuilder sb = new StringBuilder();
        try {
            while (rs.next()) {
                name = userMap.get(rs.getString("senderId"));
                time = rs.getString("nowtime");
                text = rs.getString("message");
                sb.append(name).append("~").append(time).append("~").append(text).append("&");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }//获得 "id~time~text&..." 字符串
        if (!sb.isEmpty()) sb.deleteCharAt(sb.length() - 1);//删去最后一位
        return sb.toString();
    }

    public String get_friend(String cmd) {
        String sql = "select uid,name from userlist where uid in ( " +
                "select uid2 from friend where uid1 = '" + cmd.trim() + "' union " +
                "select uid1 from friend where uid2 = '" + cmd.trim() + "' )";
        ResultSet rs = conn.getResult(sql);
        String id, name;
        StringBuilder sb = new StringBuilder();
        try {
            while (rs.next()) {
                id = rs.getString("uid");
                name = rs.getString("name");
                sb.append(id).append("~").append(name).append("&");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!sb.isEmpty()) sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    public String get_group(String cmd) {
        String sql = "select gid,gname from groups where gid in ( " +
                "select distinct gid from group_member where uid = '" + cmd + "')";
        ResultSet rs = conn.getResult(sql);
        String gid, name;
        StringBuilder sb = new StringBuilder();
        try {
            while (rs.next()) {
                gid = rs.getString("gid");
                name = rs.getString("gname");
                sb.append(gid).append("~").append(name).append("&");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if (!sb.isEmpty()) sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    public String get_member(String gid) {
        String sql = "select uid,name from userlist where uid in(" +
                "select uid from group_member where gid = '" + gid + "')";
        ResultSet rs = conn.getResult(sql);
        String id;
        String name;
        StringBuilder sb = new StringBuilder();
        try {
            while (rs.next()) {
                id = rs.getString("uid");
                name = rs.getString("name");
                sb.append(id).append("~").append(name).append("&");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if (!sb.isEmpty()) sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    public String add_account(String cmd) {//添加账号
        String[] str = cmd.split("&");
        String id = str[0];
        String password = str[1];
        String username = str[2];
        String sql = "insert into userlist(uid,password,name) " +
                "value ('" + id + "','" + password + "','" + username + "')";
        if (conn.Update(sql)) {
            userMap.put(id, username);
            return "" + true;
        } else return "" + false;
    }

    public String add_info(String cmd) {
        String[] str = cmd.split("&");
        String id1 = str[0];
        String id2 = str[1];
        String text = str[2];
        String sql = "insert into information(senderId,receiverId,message) " +
                "value ('" + id1 + "','" + id2 + "','" + text + "')";
        return "" + conn.Update(sql);
    }

    public String add_friend(String cmd) {//"id1&id2"
        String[] ids = cmd.split("&");
        String sql1 = "insert into friend value ('" + ids[0] + "','" + ids[1] + "')";
        return (conn.Update(sql1)) + "";
    }

    public String add_group(String cmd) {//uid&gid
        String[] ids = cmd.split("&");
        String sql = "insert into group_member(uid,gid) value ('" + ids[0] + "','" + ids[1] + "');";
        return conn.Update(sql) + "";
    }

    public String get_name(String cmd) {
        String sql = "select name from userlist where uid= " + cmd;
        ResultSet rs = conn.getResult(sql);
        try {
            if (rs.next()) return rs.getString("name");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return "";
    }

    public String create_group(String cmd) {//创建群聊 gname&gid
        String[] str = cmd.split("&");
        String sql = "insert into groups(gid,gname) value(" + str[1] + "," + str[0] + ")";
        return "" + conn.Update(sql);
    }
}
